package com.dlc.felear.xiaoensale.application;

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;

import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.constant.TempData;
import com.dlc.felear.xiaoensale.entity.BleRssiDevice;
import com.dlc.felear.xiaoensale.entity.MyBleWrapperCallback;
import com.dlc.felear.xiaoensale.helper.SerialManager;
import com.dlc.felear.xiaoensale.helper.Session;
import com.dlc.felear.xiaoensale.utils.LogToFile;
import com.dlc.felear.xiaoensale.utils.MyLog;
import com.dlc.felear.xiaoensale.utils.PlayVoiceUtil;
import com.itdlc.android.library.utils.APPDidUtil;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.https.HttpsUtils;
import com.zhy.http.okhttp.log.LoggerInterceptor;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

import cn.com.heaton.blelibrary.ble.Ble;
import cn.com.heaton.blelibrary.ble.BleLog;
import cn.com.heaton.blelibrary.ble.model.BleFactory;
import cn.com.heaton.blelibrary.ble.utils.UuidUtils;
import okhttp3.OkHttpClient;

public class MyApplication extends Application {

    private static final String TAG = "MyApplication";

    /**
     * 测试的硬件的ID
     */
    public static String DEVICE_ID = "default";

    public void onCreate() {
        super.onCreate();
        TempData.init(this);
        Session.init(this, Constant.SESSION_FILE_NAME);
        //捕获全局异常
        Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(this));
        //初始化设备识别码工具
        APPDidUtil.init(getApplicationContext(), "XiaoEn", "666c", "5533");
        APPDidUtil.saveAppDid("");
        DEVICE_ID = APPDidUtil.getAppDid();
        SerialManager.init(this);
        PlayVoiceUtil.getInstance(this);
        initOkHttpUtil();
        MyLog.e(TAG, "DEVICE_ID  " + DEVICE_ID);
        initBle();
//        LogToFile.getInstance();
    }

    /**
     * 初始化http请求框架
     */
    private void initOkHttpUtil() {
        HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(null, null, null);
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .sslSocketFactory(sslParams.sSLSocketFactory, sslParams.trustManager)
                .connectTimeout(30000L, TimeUnit.MILLISECONDS)
                .readTimeout(30000L, TimeUnit.MILLISECONDS)
                .addInterceptor(new LoggerInterceptor("OkHttpManager"))
                .build();
        OkHttpUtils.initClient(okHttpClient);
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    private void initBle() {
        Ble.options()
                .setLogBleEnable(true)//设置是否输出打印蓝牙日志
                .setThrowBleException(true)//设置是否抛出蓝牙异常
                .setLogTAG("AndroidBLE")//设置全局蓝牙操作日志TAG
                .setAutoConnect(false)//设置是否自动连接
                .setIgnoreRepeat(false)//设置是否过滤扫描到的设备(已扫描到的不会再次扫描)
                .setConnectFailedRetryCount(3)//连接异常时（如蓝牙协议栈错误）,重新连接次数
                .setConnectTimeout(10 * 1000)//设置连接超时时长
                .setScanPeriod(20 * 1000)//设置扫描时长
                .setMaxConnectNum(15)//最大连接数量
                .setUuidService(UUID.fromString(UuidUtils.uuid16To128("fd00")))//设置主服务的uuid
                .setUuidWriteCha(UUID.fromString(UuidUtils.uuid16To128("fd01")))//设置可写特征的uuid
                .setUuidReadCha(UUID.fromString(UuidUtils.uuid16To128("fd02")))//设置可读特征的uuid （选填）
                .setUuidNotifyCha(UUID.fromString(UuidUtils.uuid16To128("fd03")))//设置可通知特征的uuid （选填，库中默认已匹配可通知特征的uuid）
                .setFactory(new BleFactory<BleRssiDevice>() {//实现自定义BleDevice时必须设置
                    @Override
                    public BleRssiDevice create(String address, String name) {
                        return new BleRssiDevice(address, name);//自定义BleDevice的子类
                    }
                })
                .setBleWrapperCallback(new MyBleWrapperCallback())
                .create(MyApplication.this, new Ble.InitCallback() {
                    @Override
                    public void success() {
                        BleLog.e("MainApplication", "初始化成功");
                    }

                    @Override
                    public void failed(int failedCode) {
                        BleLog.e("MainApplication", "初始化失败：" + failedCode);
                    }
                });
    }


}
